<template>
  <BasicModal
    v-bind="$attrs"
    @register="registerModal"
    :title="displayModuleTitle"
    :footer="null"
    width="850px"
  >
    <BasicTable
      :data-source="tableData"
      @register="registerTable"
      :pagination="false"
      :scroll="{ x: false, y: '300px' }"
    >
      <template #toolbar>
        <a-button type="primary" @click="onAddUnitStaticInfoButtonClick">新增</a-button>
      </template>
      <template #infotype="{ column, record }">
        {{ unitStaticInfoType.filter((item) => item.value == record.infotype)[0]?.label }}
      </template>
      <template #content="{ column, record }">
        <span v-if="record.infotype == 1">{{ record.content }}</span>
        <div v-else>
          <a-image
            width="100px"
            :src="getImageFileUrlPrefix + JSON.parse(record.content).url"
            :alt="record.title"
          />
        </div>
      </template>
      <template #action="{ column, record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditUnitStaticInfoButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              label: '删除',
              popConfirm: {
                title: '是否确认删除',
                placement: 'left',
                confirm: onDeleteUnitStaticInfoButtonClick.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <AddOrUpdateUnitStaticInfo
      @register="registerAddOrUpdateUnitStaticInfoModal"
      @success="afterSuccess"
    />
  </BasicModal>
</template>
<script lang="ts">
  import { defineComponent, onMounted, ref, computed, unref } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { BasicModal, useModalInner, useModal } from '/@/components/Modal';
  import { queryEnumOptions } from '/@/api/platform/enum';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { getImageFileApi } from '/@/api/platform/file';
  import AddOrUpdateUnitStaticInfo from './AddOrUpdateUnitStaticInfo.vue';
  import { deleteUnitStaticInfo, queryUnitStaticInfo } from '/@/api/ehs/unitDivision';
  import { unitStaticInfoTableColumns } from './unitDivision.data';
  import { EhsUnitStaticInfoDto } from '/@/api/ehs/model/unitDivisionModel';
  const { createMessage } = useMessage();
  export default defineComponent({
    components: { BasicModal, BasicTable, TableAction, AddOrUpdateUnitStaticInfo },
    name: 'UnitStaticInfoModal',
    setup() {
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      // 查询静态信息的单元id
      const dataId = ref();
      const unitStaticInfoType = ref();
      const tableData = ref([]);
      const { uploadUrl } = useGlobSetting();
      const getImageFileUrlPrefix = ref<string>(uploadUrl + getImageFileApi);
      const [registerModal, { closeModal }] = useModalInner((data) => {
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (unrefData?.dataId) {
            dataId.value = unrefData.dataId;
            getTableDataValue();
          }
        }
      });
      const [
        registerAddOrUpdateUnitStaticInfoModal,
        { openModal: openAddOrUpdateUnitStaticInfoModal },
      ] = useModal();
      const [registerTable, {}] = useTable({
        columns: unitStaticInfoTableColumns,
        canResize: true,
        striped: true,
        showTableSetting: false,
        showIndexColumn: true,
        isTreeTable: false,
        bordered: true,
        ellipsis: true,
        indexColumnProps: {
          width: 50,
        },
        actionColumn: {
          width: 150,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      function getTableDataValue() {
        queryUnitStaticInfo(dataId.value).then((res) => {
          if (res) tableData.value = res;
          else tableData.value = [];
        });
      }

      /**
       * 打开新增窗口
       */

      function onAddUnitStaticInfoButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '单元静态信息维护',
          operateType: OperationTypeEnum.ADD,
          unitid: dataId.value,
        };
        openAddOrUpdateUnitStaticInfoModal(true, propsData);
      }
      /**
       * 打开编辑窗口
       * @param record
       */
      function onEditUnitStaticInfoButtonClick(record: EhsUnitStaticInfoDto) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '单元静态信息维护',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.id,
          unitid: dataId.value,
        };
        openAddOrUpdateUnitStaticInfoModal(true, propsData);
      }

      /**
       * 删除
       * @param record
       */
      function onDeleteUnitStaticInfoButtonClick(record: EhsUnitStaticInfoDto) {
        deleteUnitStaticInfo(record.id)
          .then((res) => {
            console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              afterSuccess();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }
      /**
       * 弹窗确认操作之后刷新页面
       */
      function afterSuccess() {
        getTableDataValue();
      }
      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      onMounted(() => {
        queryEnumOptions('EhsUnitStaticinfoTypeEnum').then((res) => {
          // console.log('option res', res);
          if (res != undefined && res != null && res.length > 0x0) {
            unitStaticInfoType.value = res[0].options.map((option) => {
              return {
                value: Number(option.key),
                label: option.value,
              };
            });
          }
        });
      });
      return {
        tableData,
        unitStaticInfoType,
        displayModuleTitle,
        getImageFileUrlPrefix,
        registerModal,
        registerTable,
        registerAddOrUpdateUnitStaticInfoModal,
        closeModal,
        afterSuccess,
        onAddUnitStaticInfoButtonClick,
        onEditUnitStaticInfoButtonClick,
        onDeleteUnitStaticInfoButtonClick,
      };
    },
  });
</script>
